//
//  TLLoadingView.m
//  PiyingkeLibrary
//
//  Created by Tianlong on 2017/4/7.
//
//

#import "TLLoadingView.h"
#import "TLAVPlayerPublicFile.h"

@interface TLLoadingView ()
@property (nonatomic , assign) BOOL animating;
@property (nonatomic , assign) float angle;
@end



@implementation TLLoadingView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.animating = NO;
        UIImageView *loadingC = [[UIImageView alloc] init];
        self.loadingC = loadingC;
        self.loadingC.image = [UIImage imageNamed:@"loadingC"];
        //self.loadingC.image = [UIImage imageWithBundleName:@"loadingC"];
        [self addSubview:self.loadingC];
        
        self.angle = 1;
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if(self = [super initWithCoder:aDecoder]){
        self.animating = NO;
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    WS(ws);
    CGFloat w = 60;
    [self.loadingC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(ws.mas_centerX);
        make.centerY.equalTo(ws.mas_centerY);
        make.width.offset(w);
        make.height.offset(w);
    }];
}

- (void)startLoding{
    
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
    rotationAnimation.duration = 2.0;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = CGFLOAT_MAX;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    //页面push到下一页时不停止loading，除非主动结束动画
    rotationAnimation.removedOnCompletion = NO;
    [self.loadingC.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

- (void)stopLoading{
    [self.loadingC stopAnimating];
    [self.loadingC.layer removeAllAnimations];
}



@end
